package com.exam.service.impl;

import com.exam.mapper.ChoiceQuestionMapper;
import com.exam.mapper.PaperMapMapper;
import com.exam.pojo.*;
import com.exam.service.ChoiceQuestionService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
@Service
public class ChoiceQuestionServiceImpl implements ChoiceQuestionService {
    @Resource
    private ChoiceQuestionMapper choiceQuestionMapper;

    @Resource
    private PaperMapMapper paperMapMapper;

    @Override
    public List<ChoiceQuestion> queryByPaperId(Integer paperId) {
        return choiceQuestionMapper.queryByPaperId(paperId);
    }

    @Override
    public PageInfo<ChoiceQuestion> queryPageByPaperId(Integer paperId, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<ChoiceQuestion> choiceQuestions = choiceQuestionMapper.queryByPaperId(paperId);
        PageInfo<ChoiceQuestion> pageInfo = new PageInfo<>(choiceQuestions);
        return pageInfo;
    }

    @Override
    public PageInfo<ChoiceQuestion> queryPageByQuestionCreatorId(Integer questionCreatorId, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<ChoiceQuestion> choiceQuestions = choiceQuestionMapper.queryByQuestionCreatorId(questionCreatorId);
        PageInfo<ChoiceQuestion> pageInfo = new PageInfo<>(choiceQuestions);
        return pageInfo;
    }

    @Override
    public PageInfo<ChoiceQuestion> queryAllPage(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<ChoiceQuestion> choiceQuestions = choiceQuestionMapper.queryAll();
        PageInfo<ChoiceQuestion> pageInfo = new PageInfo<>(choiceQuestions);
        return pageInfo;
    }

    @Override
    public int updateById(ChoiceQuestion choiceQuestion) {
        if (choiceQuestion.getScore() == null){
            choiceQuestion.setScore(3);
        }
        return choiceQuestionMapper.updateById(choiceQuestion);
    }

    @Override
    @Transactional
    public void deleteById(Integer id) {
        int deleteById = choiceQuestionMapper.deleteById(id);
        if (deleteById == 0) {
            throw new RuntimeException("删除选择题试题失败");
        }
        paperMapMapper.deleteByQuestionId(new PaperMap(null, 1, id));
    }

    @Override
    public int add(ChoiceQuestion choiceQuestion) {
        return choiceQuestionMapper.insert(choiceQuestion);
    }

    @Override
    public List<ComposePaper> querySubNumByQuestionCreatorId(Integer questionCreatorId) {
        return choiceQuestionMapper.querySubNumByQuestionCreatorId(questionCreatorId);
    }

    @Override
    public List<ComposePaper> querySubNum() {
        return choiceQuestionMapper.querySubNum();
    }

}
